﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Sync.LMS;
using Sync.UserServices;

namespace LoginUser.PopUpWindows
{
    /// <summary>
    /// Interaction logic for ClassChooser.xaml
    /// </summary>
    public partial class ClassChooser
    {
        readonly MainWindow _main = new MainWindow();
        private Sync.AssignmentService.Assignment _assignmentChosen;
        private ObservableCollection<Sync.AssignmentService.Course> _mycoursenames;
        private ObservableCollection<string> _myassignmentnames;
        private IEnumerable<Sync.AssignmentService.Course> selectedCourse;
        public ClassChooser()
        {
            InitializeComponent();
            ResizeMode = ResizeMode.CanMinimize;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var courseNames = new ObservableCollection<string>();
            var courseData = new ObservableCollection<Course>();
            var courses = LMSServiceUsage.GetCourses();
            var enumerator = courses.GetEnumerator();
            while(enumerator.MoveNext())
            {
                courseNames.Add(((Course)enumerator.Current).FullName);
            }
            ChooseClassComboBox.ItemsSource = courseNames;
            ChooseClassComboBox.SelectedValue = courseData;
        }

        private void ChooseClassComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _mycoursenames = LMSServiceUsage.GetAssignments();
            selectedCourse = _mycoursenames.Select(c => c).Where(c => c.FullName.Equals(ChooseClassComboBox.SelectedItem));
            var enumerator = selectedCourse.Single().Assignments.GetEnumerator();
            _myassignmentnames = new ObservableCollection<string>();
            while(enumerator.MoveNext())
            {
                _myassignmentnames.Add(((Sync.AssignmentService.Assignment)enumerator.Current).Name);
            }

            ChooseAssignentComboBox.ItemsSource = _myassignmentnames;
        }

        private void ChooseAssignentComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var enumerator = selectedCourse.Single().Assignments.GetEnumerator();
            while(enumerator.MoveNext())
            {
                if (((Sync.AssignmentService.Assignment)enumerator.Current).Name.Equals(ChooseAssignentComboBox.SelectedItem))
                    _assignmentChosen = (Sync.AssignmentService.Assignment)enumerator.Current;
                //description.Text = ((Sync.AssignmentService.Assignment) enumerator.Current).Description;
            }
        }

        private void DownloadButton_Click(object sender, RoutedEventArgs e)
        {
            //var derp = _assignmentChosen.Submissions;
            //var herp = derp.First().Files;
            //LMSServiceUsage.DownloadAssignment(_assignmentChosen.Id);
        }

        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog browse = new Microsoft.Win32.OpenFileDialog();

            if (browse.ShowDialog() == true)
            {
                string FilePath = browse.FileName;
                UploadTextBox.Text = FilePath;
            }
        }

        private void UploadButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Window_Closed(object sender, System.EventArgs e)
        {
            Visibility = Visibility.Hidden;
            MainWindow.Utilities.ShowDialog();
        }
    }
}
